using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Admin : System.Web.UI.Page
{
    SqlConnection sConn;

    SQLInteract objSQL = new SQLInteract();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Text = "";

        if (Session["UserName"] == null) //Redirect to login if session expires
        {
            Response.Redirect("Login.aspx", true);
        }
        else if (Session["UserRole"].ToString() == "Admin" || Session["UserRole"].ToString() == "Administrator")
        {
            //Response.Redirect("Admin.aspx");
        }
        else
        {
            
            Response.Redirect("Home.aspx");
            
        }
        if (!IsPostBack)
        {
            //GridPan.Visible = false;
        }  
    }
    public void DbConnection()
    {
        sConn = new SqlConnection(ConfigurationManager.ConnectionStrings["PortalConnectionString"].ConnectionString);
        sConn.Open();
    }
    protected void EditMenu_Click(object sender, EventArgs e)
    {
        PanelField.Controls.Clear();
        GridViewMenubind();
        PanelField.Visible = false;
        PanelMenu.Visible = true;
        
    }
    protected void EditField_Click(object sender, EventArgs e)
    {
        
        PanelMenu.Controls.Clear();
        GridViewFieldbind();
        PanelField.Visible = true;
        PanelMenu.Visible = false;
    }
    protected void GridViewMenu_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void GridViewMenu_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        lblMessage.Text = "Record Deleted";
    }
    protected void GridViewMenu_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        lblMessage.Text = "Record Updated";
    }
    protected void SqlDataSourceMenu_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
        lblMessage.Text = "Record Inserted";
    }
    /// <summary>
    /// Delete Menu from database
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridViewMenu_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DbConnection();
        GridViewRow row = (GridViewRow)GridViewMenu.Rows[e.RowIndex];
        Label lbldeleteID = (Label)row.FindControl("lblMenuID");
        string getDeletingItem = "select [Text], [Description] FROM [AvivaTPortal].[dbo].[Menu] where [MenuID]=" + lbldeleteID.Text + "";
        DataSet ds= new DataSet();
        ds= objSQL.ReturnDataAdapter(getDeletingItem);
        if (ds.Tables[0].Rows[0][0].ToString() != "Admin")
        {
            string deleteItem = "DELETE FROM [AvivaTPortal].[dbo].[Menu] where [MenuID]=" + lbldeleteID.Text + "";
            SqlCommand cmd = new SqlCommand(deleteItem, sConn);
            cmd.ExecuteNonQuery();
        }
        else
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "We can delete Admin Page. For This Please Contact Administrator";
        }
        sConn.Close();
        GridViewMenubind();
    }
    /// <summary>
    /// Update Menu
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridViewMenu_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DbConnection();
        GridViewRow row = (GridViewRow)GridViewMenu.Rows[e.RowIndex];

        Label lbl = (Label)row.FindControl("lblMenuID");
        TextBox txMenuName = (TextBox)row.FindControl("txtMenuText");
        TextBox texMenuDesc = (TextBox)row.FindControl("txtMenuDesc");
        TextBox texParentID = (TextBox)row.FindControl("txtParentID");
        TextBox texBusType = (TextBox)row.FindControl("txtBusType");

        GridViewMenu.EditIndex = -1;
        if (texBusType.Text == "")
        {
            texBusType.Text = null;
        }
        if (texParentID.Text == "")
        {
            texParentID.Text = "Null";
        }
        string updateMenuQ = "UPDATE [AvivaTPortal].[dbo].[Menu]" + " " +
            "SET [Text] = '" + txMenuName.Text + "'" + " , " +
            "[Description] = '" + texMenuDesc.Text + "'" + " ," +
            "[ParentID] = " + texParentID.Text + "" + ", " +
            "[BUS_TYPE] = '" + texBusType.Text + "'" + " " +
            "WHERE [MenuID]=" + (lbl.Text) + "";

        SqlCommand cmd = new SqlCommand(updateMenuQ, sConn);
        cmd.ExecuteNonQuery();
        sConn.Close();

        GridViewMenubind();
    }
    protected void GridViewMenu_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridViewMenu.EditIndex = -1;
        GridViewMenubind();
    }
    protected void GridViewMenu_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewMenu.EditIndex = e.NewEditIndex;
        GridViewMenubind();
    }

    public void GridViewMenubind()
    {

        DbConnection();
        string selectMenuQ = "SELECT [MenuID],[Text],[Description],[ParentID],[BUS_TYPE]FROM [AvivaTPortal].[dbo].[Menu]";
        SqlDataAdapter AdapMenu = new SqlDataAdapter(selectMenuQ, sConn);
        DataTable dtMenu = new DataTable();
        AdapMenu.Fill(dtMenu);
        GridViewMenu.DataSource = dtMenu;
        GridViewMenu.DataBind();
        sConn.Close();
    }
    
    public void AddNewMenu(object sender, EventArgs e)
    {
        //this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" > alert(\"Error: Hello Edit Me\");</script>", false);
        DbConnection();
        //Label lbl = (Label)row.FindControl("lblMenuID");
        TextBox txMenuName = (TextBox)GridViewMenu.FooterRow.FindControl("txtMText");
        TextBox texMenuDesc = (TextBox)GridViewMenu.FooterRow.FindControl("txtMDesc");
        TextBox texParentID = (TextBox)GridViewMenu.FooterRow.FindControl("txtPID");
        TextBox texBusType = (TextBox)GridViewMenu.FooterRow.FindControl("txtBType");
        if (texBusType.Text == "")
        {
            texBusType.Text = null;
        }
        if (texParentID.Text == "")
        {
            texParentID.Text = "Null";
        }
        string InsertMenuQ = "INSERT INTO [AvivaTPortal].[dbo].[Menu]([Text],[Description],[ParentID],[BUS_TYPE])" +
                             "VALUES('" + txMenuName.Text.ToString().Trim() + "','" + texMenuDesc.Text.ToString().Trim() + "'," + texParentID.Text + ",'" + texBusType.Text.ToString().Trim() + "')";

        SqlCommand cmdInsert = new SqlCommand(InsertMenuQ, sConn);
        cmdInsert.ExecuteNonQuery();
        sConn.Close();

        GridViewMenubind();
    }
   
    public void GridViewFieldbind()
    {

        DbConnection();
        string selectFieldQ = "SELECT [FID],[FTEXT],[F_DEPENDENT_ON],[BUS_TYPE] FROM [AvivaTPortal].[dbo].[TC_FEILDS]";
        SqlDataAdapter AdapField = new SqlDataAdapter(selectFieldQ, sConn);
        DataTable dtField = new DataTable();
        AdapField.Fill(dtField);
        GridViewField.DataSource = dtField;
        GridViewField.DataBind();
        sConn.Close();
    }
    protected void GridViewField_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridViewField.EditIndex = -1;
        GridViewFieldbind();
    }
    protected void GridViewField_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void GridViewField_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DbConnection();
        GridViewRow row = (GridViewRow)GridViewField.Rows[e.RowIndex];
        Label lbldeleteFID = (Label)row.FindControl("lblFieldID");
        string deleteItem = "DELETE FROM [AvivaTPortal].[dbo].[TC_FEILDS] where [FID]=" + lbldeleteFID.Text + "";
        SqlCommand cmd = new SqlCommand(deleteItem, sConn);
        cmd.ExecuteNonQuery();
        sConn.Close();
        GridViewFieldbind();
    }
    protected void GridViewField_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        lblMessage.Text = "Record Deleted";
    }
    protected void GridViewField_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewField.EditIndex = e.NewEditIndex;
        GridViewFieldbind();
    }

    protected void GridViewField_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        lblMessage.Text = "Record Updated";
    }
    protected void GridViewField_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DbConnection();
        GridViewRow row = (GridViewRow)GridViewField.Rows[e.RowIndex];

        Label lbl = (Label)row.FindControl("lblFieldID");
        TextBox texFieldName = (TextBox)row.FindControl("txtFieldText");
        TextBox texFieldDepend = (TextBox)row.FindControl("txtFieldDepend");
        TextBox texBusType = (TextBox)row.FindControl("txtBusType");


        GridViewField.EditIndex = -1;
        if (texBusType.Text == "")
        {
            //set msg
        }
        if (texFieldDepend.Text == "")
        {
            texFieldDepend.Text = "Null";
        }
        string updateMenuQ = "UPDATE [AvivaTPortal].[dbo].[TC_FEILDS]" + " " +
            "SET [FTEXT] = '" + texFieldName.Text.ToString().Trim() + "'" + " , " +
            "[F_DEPENDENT_ON] = " + texFieldDepend.Text.Trim() + "" + " ," +
            "[BUS_TYPE] = '" + texBusType.Text.ToString().Trim() + "'" + " " +

            "WHERE [FID]=" + lbl.Text.Trim() + "";

        SqlCommand cmd = new SqlCommand(updateMenuQ, sConn);
        cmd.ExecuteNonQuery();
        sConn.Close();
        GridViewFieldbind();
        
    }
    public void AddNewField(object sender, EventArgs e)
    {
        //this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" > alert(\"Error: Hello Edit Me\");</script>", false);
        DbConnection();
        //Label lbl = (Label)row.FindControl("lblMenuID");
        TextBox texFieldName = (TextBox)GridViewField.FooterRow.FindControl("txtFText");
        TextBox texFieldDepend = (TextBox)GridViewField.FooterRow.FindControl("txtFDepend");
        TextBox texBusType = (TextBox)GridViewField.FooterRow.FindControl("txtBType");

        if (texBusType.Text == "")
        {
            lblMessage.Text = "Please Provide Business Name";
            texBusType.Focus();
        }
        if (texFieldDepend.Text == "")
        {
            texFieldDepend.Text = "Null";
        }
        string InsertMenuQ = "INSERT INTO [AvivaTPortal].[dbo].[TC_FEILDS]([FTEXT],[F_DEPENDENT_ON],[BUS_TYPE])" +
                             "VALUES('" + texFieldName.Text.ToString().Trim() + "'," + texFieldDepend.Text.ToString().Trim() + ",'" + texBusType.Text.ToString().Trim() + "')";

        SqlCommand cmdInsert = new SqlCommand(InsertMenuQ, sConn);
        cmdInsert.ExecuteNonQuery();
        sConn.Close();
        GridViewFieldbind();
    }
}
